%11019

ftimes=get_frame_times(aux_data(2,:));
ftindex=1:length(ftimes);
time=ftindex/40; 
time(150001)=[];

figure, plot(time,proj_meta.rd.ps_id-5)
hold on
plot(time,proj_meta.rd.velM_smoothed,'r')
plot(time,proj_meta.rd.velP_smoothed,'g')
set(gca,'ylim',[-0.15 0.15])
set(gca,'xlim',[0 150000/40])
box off

figure, imagesc(proj_meta.rd.act)
colorbar
set(gca,'clim',[1 4])
box off


%%% pertubations

angle=aux_data(10,:);
angle_ft=angle(ftimes(ftindex));
angle_ft(150001)=[];
angle_ft_sm=smooth2(angle_ft,10);

figure, plot(time,angle_ft,'k')
hold on
plot(time,proj_meta.rd.ps_id-5,'b')
set(gca,'ylim', [-1 1],'xlim',[2720/40 8290/40])
box off

rewind=proj_meta.rd.ps_id>5.0002;
rew_onset=find(diff(rewind==1));
rew_onset(logical([0 diff(rew_onset)<2]))=[]
pind_L=proj_meta.rd.ps_id<4.9998;
p_onset_L=find(diff(pind_L==1));
p_onset_L(logical([0 diff(p_onset_L)<2]))=[]

win = 200

rew_act=struct

for knd=1:size(proj_meta.rd.act,1)
    for ind=1:length(rew_onset)
      rew_act.R(knd).PST(ind,:)=proj_meta.rd.act(knd,rew_onset(ind)-win:rew_onset(ind)+win);
    end
end
for knd=1:size(proj_meta.rd.act,1)
    for ind=1:length(p_onset_L)
      rew_act.L(knd).PST(ind,:)=proj_meta.rd.act(knd,p_onset_L(ind)-win:p_onset_L(ind)+win);
    end
end

cmaps_R=hsv(size(rew_act.R(1).PST,1));
cmaps_L=hsv(size(rew_act.L(1).PST,1));

for xnd =1:length(rew_act.R)
    figure,
    hold on
    for mnd = 1:size(rew_act.R(1).PST,1)
        plot(time(1:2*win+1),rew_act.R(xnd).PST(mnd,:),'Color',cmaps_R(mnd,:))
    end
    plot([win/40 win/40],[0.5 6],'r','LineWidth',2)
    plot(time(1:2*win+1),mean(rew_act.R(xnd).PST,1),'k','LineWidth',2)
    set(gca,'xlim',[0 2*win/40])
    box off
end

for ynd = 1:length(rew_act.L)
    figure,
    hold on
    for mnd = 1:size(rew_act.L(1).PST,1)
        plot(time(1:2*win+1),rew_act.L(ynd).PST(mnd,:),'Color',cmaps_L(mnd,:))
    end
    plot([win/40 win/40],[0.5 6],'r','LineWidth',2)
    plot(time(1:2*win+1),mean(rew_act.L(ynd).PST,1),'k','LineWidth',2)
    set(gca,'xlim',[0 2*win/40])
    box off
end

figure,
hold on
for and = 1:length(rew_act.R)
    for mnd = 1:size(rew_act.R(1).PST,1)
        plot(time(1:2*win+1),rew_act.R(and).PST(mnd,:),'Color',[.5 .5 .5])
    end
    for mnd = 1:size(rew_act.L(1).PST,1)
        plot(time(1:2*win+1),rew_act.L(and).PST(mnd,:),'Color',[0.5 0.5 1])
    end
    ps_act_R_mean(and,:)=mean(rew_act.R(and).PST(1:length(rew_onset),:));
    ps_act_L_mean(and,:)=mean(rew_act.L(and).PST(1:length(p_onset_L),:));
end
plot(time(1:2*win+1),mean(ps_act_R_mean),'k','LineWidth',2)
plot(time(1:2*win+1),mean(ps_act_L_mean),'b','LineWidth',2)
plot([win/40 win/40],[0.5 6],'r','LineWidth',2)
set(gca,'xlim',[0 2*win/40])

figure,
hold on
for and = 1:length(rew_act.R)
    plot(1,mean(ps_act_R_mean(and,1:win),2),'s')
    plot(1.1,mean(ps_act_R_mean(and,win:2*win),2),'s')
    plot([1 1.1], [mean(ps_act_R_mean(and,1:win),2) mean(ps_act_R_mean(and,win:2*win),2)],':')
end
for and = 1:length(rew_act.L)
    plot(1.2,mean(ps_act_L_mean(and,1:win),2),'rs')
    plot(1.3,mean(ps_act_L_mean(and,win:2*win),2),'rs')
    plot([1.2 1.3], [mean(ps_act_L_mean(and,1:win),2) mean(ps_act_L_mean(and,win:2*win),2)],':r')
end
plot(1,mean(mean(ps_act_R_mean(1:length(rew_act.R),1:win))),'ks','LineWidth',2)
plot(1.1,mean(mean(ps_act_R_mean(1:length(rew_act.R),win:2*win))),'ks','LineWidth',2)
plot([1 1.1], [mean(mean(ps_act_R_mean(1:length(rew_act.R),1:win))) mean(mean(ps_act_R_mean(1:length(rew_act.R),win:2*win)))],'k')
plot(1.2,mean(mean(ps_act_L_mean(1:length(rew_act.L),1:win))),'gs','LineWidth',2)
plot(1.3,mean(mean(ps_act_L_mean(1:length(rew_act.L),win:2*win))),'gs','LineWidth',2)
plot([1.2 1.3], [mean(mean(ps_act_L_mean(1:length(rew_act.L),1:win))) mean(mean(ps_act_L_mean(1:length(rew_act.L),win:2*win)))],'g')
set(gca, 'ylim', [0.90 1.3], 'xlim', [0.9 1.4])
box off

%%% running onsets

figure, plot(proj_meta.rd.ps_id-5)
hold on
plot(proj_meta.rd.velM_smoothed,'r')
plot(r_onset,0,'.g')
set(gca,'ylim',[-0.15 0.15])
set(gca,'xlim',[0 150000])
box off

rind=[];
rind=proj_meta.rd.velM_smoothed<-0.005;
aaa=diff(rind==1);
r_onset=find(diff(aaa>0));
r_onset(logical([0 diff(r_onset)<2]))=[];

win = 1000 %peri-stim window
r_act=struct

for knd=1:size(proj_meta.rd.act,1)
    for ind=1:length(r_onset)
      r_act.c(knd).PST(ind,:)=proj_meta.rd.act(knd,r_onset(ind)-win:r_onset(ind)+win);
    end
end

cmaps_run=hsv(size(r_act.c(1).PST,1));

for xnd = [5 13 16 19 27 28 30]
    figure,
    hold on
    for mnd = 1:size(r_act.c(1).PST,1)
        plot(time(1:2*win+1),r_act.c(xnd).PST(mnd,:),'Color',cmaps_run(mnd,:))
    end
    plot([win/40 win/40],[0.5 6],'r','LineWidth',2)
    plot(time(1:2*win+1),mean(r_act.c(xnd).PST,1),'k','LineWidth',2)
    set(gca,'xlim',[0 2*win/40])
end

figure,
hold on
for and = 1:length(r_act.c)
    for mnd = 1:size(r_act.c(1).PST,1)
        plot(time(1:2*win+1),r_act.c(and).PST(mnd,:),'Color',[.8 .8 .8])
    end
    r_act_c_mean(and,:)=mean(r_act.c(and).PST(1:length(r_onset),:));
end
plot(time(1:2*win+1),mean(r_act_c_mean),'k','LineWidth',2)
plot([win/40 win/40],[0.5 6],'r','LineWidth',2)
set(gca,'xlim',[0 2*win/40])
box off
set(gca,'ylim',[1 1.08])

win2av=1000

figure,
hold on
for and = 1:length(r_act.c)
    plot(1,mean(r_act_c_mean(and,1:win2av),2),'s')
    plot(1.1,mean(r_act_c_mean(and,win2av:2*win2av),2),'s')
    plot([1 1.1], [mean(r_act_c_mean(and,1:win2av),2) mean(r_act_c_mean(and,win2av:2*win2av),2)],':')
end
plot(1,mean(mean(r_act_c_mean(1:length(r_act.c),1:win2av))),'rs','LineWidth',2)
plot(1.1,mean(mean(r_act_c_mean(1:length(r_act.c),win2av:2*win2av))),'rs','LineWidth',2)
plot([1 1.1], [mean(mean(r_act_c_mean(1:length(r_act.c),1:win2av))) mean(mean(r_act_c_mean(1:length(r_act.c),win2av:2*win2av)))],'r')
set(gca, 'ylim', [1 1.2], 'xlim', [0.95 1.15])
box off

